#!/usr/bin/env python
#coding:utf8
from db import user_info

import(sys)
reload(sys)
sys.setdefaultencoding("utf8")

class User:
	"""docstring for ClassName"""
	def __init__(self, arg):
		pass
		
	def check_user_exist(self,phone):
		if phone in user_info.keys():
			return True
		else:
			return False

	def check_user_active(self,phone):
		if user_info[phone]["status"]:
			return True
		else:
			return False

    def check_user_pass(self,phone,passwd):
    	if user_info[phone]["password"] == passwd:
    		return True
    	else:
    		return False

    #输入密码次数
    
    def login(self,phone,passwd):
    	if self.check_user_exist(phone):
    		print "check_user_exist success"
    		if self.check_user_active(phone):
    			if self.check_user_pass(phone,passwd):
    				return {"message" : "登录成功"}
    			else:
    				return {"message" : "密码错误"}
    		else:
    			return {"message" : "用户未激活"}
    	else:
    		return {"message" : "用户不存在"}



    def check_special_char(self,phone):
    	char = set(["'","/","\\"])
    	phone = set(phone)
    	if char & phone:
    		return False
    	else:
    		return True

    #确认是否被注册过
    #login.check_user_exist()


    def reg_action(self,phone,passwd,stage_class,username):
        #commit reg
        user_info[phone] = {
            "id":2,
            "password":passwd,
            "stage_class":stage_class,
            "username":username,
            "status":1
        }
        print user_info
        return True

    def reg(self,phone,passwd,stage_class,username):
    	if self.check_special_char(username):
    		if self.check_special_char(passwd):
    			if login.check_user_exist():
    				if self.reg_action(phone,passwd,stage_class,username):
    					print "注册成功"
    		        else:
    		        	print "系统存在错误，请联系管理员或稍后重试。\
                                错误代码：201"
                else:
                	print "帐号已存在"
            else:
            	print "密码存在特殊字符，请重新输入"
        else:
        	print "用户名存在特殊字符，请重新输入"
